﻿using System;

namespace TPG.SharePoint.Workflow
{
    internal static class EnumUtils
    {
        internal static TEnum SafeParseEnum<TEnum>(string stringValue)
        {
            TEnum value = default(TEnum);

            if (String.IsNullOrEmpty(stringValue) == false)
            {
                if (Enum.IsDefined(typeof(TEnum), stringValue) == false)
                {
                    throw new WorkflowException(
                        String.Format("{0} {1} not defined",
                        typeof(TEnum).Name, stringValue));
                }
                value = (TEnum)Enum.Parse(typeof(TEnum), stringValue);
            }
            return value;
        }
    }
}
